/*
 * @Author: Jiraiya
 * @Date: 2020-02-28 22:44:47
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-02-28 23:05:08
 * @Description: 
 */

l = console.log

new RegExp(/\d+/, 'g')
//等同于
new RegExp(/\d+/g)

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

//u修饰符 正确识别四个字符的大码点
/^\uD83D/u.test('\uD83D\uDC2A') //false
/^\uD83D/.test('\uD83D\uDC2A') //true


var s = '𠮷'
/^.$/u.test(s) //true
/^.$/.test(s) //true


//unicode字符匹配用花括号u{}
/u{61}/.test('a') //true


//unicode判断是否有u修饰符
const r1 = /hello/u
r1.unicode //true

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

//y黏连修饰符和g一样但是每次都从下一次的地方匹配
const s = 'a_aa_xaaa_'
const r1 = /a+_/g
const r2 = /a+_/y

let match1 = ''
while(match1 = r1.exec(s)){
    l(match1)
}
//[ 'a_', index: 0, input: 'a_aa_xaaa_', groups: undefined ]
//[ 'aa_', index: 2, input: 'a_aa_xaaa_', groups: undefined ]
//[ 'aaa_', index: 6, input: 'a_aa_xaaa_', groups: undefined ]

let match2 = ''
while(match2 = r2.exec(s)){
    l(match2)
}
//[ 'a_', index: 0, input: 'a_aa_xaaa_', groups: undefined ]
//[ 'aa_', index: 2, input: 'a_aa_xaaa_', groups: undefined ]

//sticky判断正则是否有y修饰符
r2.sticky // true

//flags返回正则修饰符
r2.flags //y
l(r2.source) //a+_

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

//.字符可以匹配除了终止符之外的任何字符[\n\r]
//s修饰符可以让.匹配所有字符
const s = 'foo\nbar'
const r1 = /foo.bar/
const r2 = /foo.bar/s

l(r1.test(s))//false
l(r2.test(s))//true

//dotAll判断正则是否处于匹配一切
l(r1.dotAll, r2.dotAll)// false true

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

const s = '1-￥2-3-4￥'

//先行断言 /x(?=y)/ 只有x在y前面才匹配y
const r1 = /\d+(?=￥)/g
l(s.match(r1)) // ['4']

//先行否定断言 /x(?!y)/ 只有x不在y前面全部匹
const r2 = /\d+(?!￥)/g
l(s.match(r2)) // [’1‘, '2', '3']

//后行断言 /(?<=y)x/ 只有x在y后面才匹配
const r3 = /(?<=￥)\d+/g
l(s.match(r3)) // ['2']

//后行否定断言 /(?<!y)x/ 只有x不在y后面才匹配
const r4 = /(?<!￥)\d+/g
l(s.match(r4)) // ['1', '3', '4']

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

// p属性配置unicode属性具体查阅

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

//具名组匹配 对于匹配到的值分配key (?<组名>)

const re_data1 = /(\d{4})-(\d{2})-(\d{2})/
const match_obj1 = re_data1.exec('2020-02-21')
l(match_obj1.groups)
//undefined

const re_data2 = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const match_obj2 = re_data2.exec('2020-02-21')
l(match_obj2.groups)
//{ year: '2020', month: '02', day: '21' }

//可以解构
let {groups: {year, month, day}} = re_data2.exec('2020-02-21')
l(year, month, day)
// 2020 02 21

//replace
'2015-02-11'.replace(re_data2, function(
    matched, //'2015-02-11'
    capture1, //'2015'
    capture2, //'02'
    capture3, //'11'
    position, // 0
    originStr, //'2015-02-11'
    groups,
){
    const {year, month, day} = groups;
    l(`${year}/${month}/${day}`)
    // 2015/02/11
})

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

// 正则引用具名组 \k<组名> \1也行
// 正则匹配到的直接在记下来匹配引用

const r = /^(?<word>[a-z]+)!\k<word>$/
const str1 = 'abc!abc'
const str2 = 'abc!ab'
l(r.test(str1)) //true
l(r.test(str2)) //false

const r = /^(?<word>[a-z]+)!\1$/
const str1 = 'abc!abc'
const str2 = 'abc!ab'
l(r.test(str1)) //true
l(r.test(str2)) //false

//🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏